/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server.votesending;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import server.votesending.shamirsecret.GenerateKeysAndSecret;

public class TCPEchoServerExecutor implements Runnable {

    public boolean work;
    
    public TCPEchoServerExecutor(){
        work = true;
    }
    
    public void stop(){
        work = false;
    }
    public static void main(String[] args) throws IOException {
        int echoServPort = 3737; // Server port

        // Create a server socket to accept client connection requests
        ServerSocket servSock = new ServerSocket(echoServPort);

        Logger logger = Logger.getLogger("practical");

        Executor service = Executors.newCachedThreadPool();  // Dispatch svc

        // Run forever, accepting and spawning threads to service each connection
        while (true) {
            Socket clntSock = servSock.accept(); // Block waiting for connection
            service.execute(new EchoProtocol(clntSock, logger));
        }
    /* NOT REACHED */
    }

    @Override
    public void run() {
        try {
            int echoServPort = 37; // Server port

            // Create a server socket to accept client connection requests
            ServerSocket servSock = new ServerSocket(echoServPort);

            Logger logger = Logger.getLogger("practical");

            Executor service = Executors.newCachedThreadPool();  // Dispatch svc

            // Run forever, accepting and spawning threads to service each connection
            while (work) {
                Socket clntSock = servSock.accept(); // Block waiting for connection
                service.execute(new EchoProtocol(clntSock, logger));
            }
        } catch (IOException ex) {
            Logger.getLogger(TCPEchoServerExecutor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
